/**
 * 给你两个 非空 的链表，表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的，并且每个节点只能存储 一位 数字。
 *
 * 输入：l1 = [2,4,3], l2 = [5,6,4]
 * 输出：[7,0,8]
 * 解释：342 + 465 = 807.
 */

class LinkNode {
    constructor(val, next ) {
        this.val = val;
        this.next = next === undefined ? null: next
    }
}

const L1 = {
    val: 1,
    next: {
        val: 2,
        next: {
            val: 3,
            next: null
        }
    }
}

const L2 = {
    val: 9,
    next: {
        val: 3,
        next: {
            val: 10,
            next: {
                val: 6,
                next: null
            }
        }
    }
}

function twoAdd(L1, L2) {
    if(!L1 && !L2) return null;
    let tag = 0
    let head = new LinkNode(0)
    while(L1 || L2) {
        let sum = 0;
        if(L1 !== null) {
            sum += L1.val
            L1 = L1.next
        }
        if(L2 !== null) {
            sum += L2.val
            L2 = L2.next
        }
        // 这里是否进位
        sum += tag
        tag = Math.floor(sum /10)
        sum = sum % 10
        head.next = new LinkNode(sum)
        head = head.next
    }
    if(tag > 0) {
        head.next= 1
    }
    return  head;
}

console.log(twoAdd(L1, L2));
